# File for the post preds of the second stage
# 10.11.12
# Updated in July 2013 for the inclusion of imputation error



##############
# Functions
##############




#### Subsetted Model for Popoularity


pdf("../figures/s2_pop_fd_cmc.pdf", height = 5, width = 3.8)
par(mar = c(3,5,.4,.4))
plot(1,1, type = "n"
		, bty = "n"
		, xlim = c(0, 3)
		, ylim = c(-2000,10000)
		, xaxt = "n" , yaxt = "n"
		, xlab = " "
		, ylab = " "
		)
#axes etc.
axis(2, las = 1)
mtext('Expected Duration Difference (days)', 2, line = 4)
abline(h=0, col = oxred, lwd = 2, lty = 2)		
lines(c(1,1), c(cmc[2], cmc[3]), lwd = 3, col = oxblue)
lines(c(2,2), c(ncmc[2], ncmc[3]), lwd = 3, col = oxblue)
points(c(1,2),c(cmc[1], ncmc[1]), pch = 16, col = c(oxblue), cex = 1.8)
mtext(at = c(1,2)
	,text = c("CMC \n "
			, "CMG & TC \n")
	, side = 1, line = 1, cex = .95
)
dev.off()






pdf("../figures/s2_pop_fd_gc.pdf", height = 5, width = 3.8)
par(mar = c(3,5,.4,.4))
plot(1,1, type = "n"
		, bty = "n"
		, xlim = c(0, 3)
		, ylim = c(-2000,10000)
		, xaxt = "n" , yaxt = "n"
		, xlab = " "
		, ylab = " "
		)
#axes etc.
axis(2, las = 1)
mtext('Expected Duration Difference (days)', 2, line = 4)
abline(h=0, col = oxred, lwd = 2, lty = 2)		
lines(c(1,1), c(cmc.gc[2], cmc.gc[3]), lwd = 3, col = oxblue)
lines(c(2,2), c(ncmc.gc[2], ncmc.gc[3]), lwd = 3, col = oxblue)
points(c(1,2),c(cmc.gc[1], ncmc.gc[1]), pch = 16, col = c(oxblue), cex = 1.8)
mtext(at = c(1,2)
	,text = c("CMC \n "
			, "CMG & TC \n")
	, side = 1, line = 1, cex = .95
)
dev.off()




pdf("../figures/s2_pop_fd_tc.pdf", height = 5, width = 3.8)
par(mar = c(3,5,.4,.4))
plot(1,1, type = "n"
		, bty = "n"
		, xlim = c(0, 3)
		, ylim = c(-2000,10000)
		, xaxt = "n" , yaxt = "n"
		, xlab = " "
		, ylab = " "
		)
#axes etc.
axis(2, las = 1)
mtext('Expected Duration Difference (days)', 2, line = 4)
abline(h=0, col = oxred, lwd = 2, lty = 2)		
lines(c(1,1), c(cmc.tc[2], cmc.tc[3]), lwd = 3, col = oxblue)
lines(c(2,2), c(ncmc.tc[2], ncmc.tc[3]), lwd = 3, col = oxblue)
points(c(1,2),c(cmc.tc[1], ncmc.tc[1]), pch = 16, col = c(oxblue), cex = 1.8)
mtext(at = c(1,2)
	,text = c("CMC \n "
			, "CMG & TC \n")
	, side = 1, line = 1, cex = .95
)
dev.off()




















######################
# compares govt cooperation and other policies

##pdf("../figures/s2_pop_fd.pdf", width = 6, height = 5)
#par(mar = c(3,5,.4,.4))
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, 5)
#		, ylim = c(-5000,16000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = " "
#		, ylab = " "
#		)
##axes etc.
#axis(2, las = 1)
#mtext('Expected Duration Difference (days)', 2, line = 4)
#abline(h=0, col = oxred, lwd = 2, lty = 2)		
#lines(c(1.1,1.1), c(cmc.gc[2], cmc.gc[3]), lwd = 3, col = oxgrey)
#lines(c(1.9,1.9), c(ncmc.gc[2], ncmc.gc[3]), lwd = 3, col = oxblue)
#lines(c(3.6,3.6), c(cmc.ngc[2], cmc.ngc[3]), lwd = 3, col = oxgrey)
#lines(c(4.4,4.4), c(ncmc.ngc[2], ncmc.ngc[3]), lwd = 3, col = oxblue)
#points(c(1.1,1.9,3.6,4.4),c(cmc.gc[1], ncmc.gc[1], cmc.ngc[1], ncmc.ngc[1]), pch = 16, col = c(oxgrey, oxblue), cex = 1.8)
#mtext('Government \n Cooperation', 1, at = 1.5, line = 1)
#mtext('Other \n Policies', 1, at = 4, line = 1)
#legend("topright"
#  , legend = c('Common Market Council', 'Common Market Group \n & Trade Commission'), box.col ='white'
#  , col = c(oxgrey, oxblue)
#  , cex = .8
#  , pch = 16
#  , lty = 1
#  , lwd = 2
#)
#
##dev.off()


















##### out2.tex version old
## TE
## CMC
#te.cmc.h <- high(te.sims.cmc)
#te.cmc.m <- mean(te.sims.cmc)
#te.cmc.l <-  low(te.sims.cmc)
## Not CMC
#te.ncmc.h <- high(te.sims.ncmc)
#te.ncmc.m <- mean(te.sims.ncmc)
#te.ncmc.l <-  low(te.sims.ncmc)
#
## GC
## CMC
#gc.cmc.h <- apply(gc.sims.cmc, 2, high)
#gc.cmc.m <- apply(gc.sims.cmc, 2, mean)
#gc.cmc.l <-  apply(gc.sims.cmc, 2, low)
## Not CMC
#gc.ncmc.h <- apply(gc.sims.ncmc, 2, high)
#gc.ncmc.m <- apply(gc.sims.ncmc, 2, mean)
#gc.ncmc.l <-  apply(gc.sims.ncmc, 2, low)
#
#
#
## Plot Trade Exception FD
#
#pdf("../figures/s2_pop_fd.pdf", width = 6, height = 5)
#par(mar = c(3,5,.4,.4))
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, 5)
#		, ylim = c(-5000,16000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = " "
#		, ylab = " "
#		)
##axes etc.
#axis(2, las = 1)
#mtext('Expected Duration Difference (days)', 2, line = 4)
#abline(h=0, col = oxred, lwd = 2, lty = 2)		
#lines(c(1.1,1.1), c(gc.cmc.h, gc.cmc.l), lwd = 3, col = oxgrey)
#lines(c(1.9,1.9), c(gc.ncmc.h, gc.ncmc.l), lwd = 3, col = oxblue)
#lines(c(3.6,3.6), c(te.cmc.h, te.cmc.l), lwd = 3, col = oxgrey)
#lines(c(4.4,4.4), c(te.ncmc.h, te.ncmc.l), lwd = 3, col = oxblue)
#points(c(1.1,1.9,3.6,4.4),c(gc.cmc.m, gc.ncmc.m, te.cmc.m, te.ncmc.m), pch = 16, col = c(oxgrey, oxblue), cex = 1.8)
#mtext('Government \n Cooperation', 1, at = 1.5, line = 1)
#mtext('Exceptions Common \n External Tariff', 1, at = 4, line = 1)
#legend("topright"
#  , legend = c('Common Market Council', 'Common Market Group \n & Trade Commission'), box.col ='white'
#  , col = c(oxgrey, oxblue)
#  , cex = .8
#  , pch = 16
#  , lty = 1
#  , lwd = 2
#)
#
#dev.off()

























############### OLD ################################################

# Switch between all interactions and only one
#weib.pop.m <- weib.pop.m2



##################
# Calculations
##################


### Simulatins included in 'analysis for sims'

## test
#### Government Cooperation
#weib.pop.gc.cmc.x <- setx(weib.pop.gc1, prom=proml, gmc = 0, tc = 0)
#weib.pop.gc.cmc.s <- sim(weib.pop.gc1, x=weib.pop.gc.cmc.x, num = 1e4)
#weib.pop.gc.ncmc.x <- setx(weib.pop.gc1, prom=proml, gmc = 1, tc = 1)
#weib.pop.gc.ncmc.s <- sim(weib.pop.gc1, x=weib.pop.gc.ncmc.x, num = 1e4)

## background
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, length(proml))
#		, ylim = c(0,10000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = "Mercosur's Popular Support (%)"
#		, ylab = "Expected Incorporation Duration (days)"
#		)
##axes etc.
#axis(1
#		, at = c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))
#		, lab = c('70', '75','80', '85','90')
#		)
#axis(2, at= c(0,2500, 5000, 7500, 10000), las = 1)		
## Grids
#for (i in seq(0, length(proml), .25* length(proml))){lines(c(i,i),c(-200,12000), col = "grey50", lty = 3)}  #vertical grid lines
#for (i in seq(0, 10000, 2500)){lines(c(-5,length(proml)),c(i,i), col = "grey50", lty = 3)}	# hor. grid lines
## Add the shades
#add.shade(weib.pop.gc.cmc.s, proml, oxblue)
#lines(apply(weib.pop.gc.cmc.s$qi$ev, 2, low)[0:40], col = 'white' , lwd = 3, lty = 1)	
#add.shade(weib.pop.gc.ncmc.s, proml, oxgrey)
#lines(apply(weib.pop.gc.cmc.s$qi$ev, 2, low)[0:80], col = 'white' , lwd = 3, lty = 3)	
#lines(apply(weib.pop.gc.cmc.s$qi$ev, 2, mean), col = 'white' , lwd = 3, lty = 2)	
##add.shade(weib.pop.tc.s, pop, oxgrey)
#










############
# Plotting
############

## Plot Government Cooperation
#pdf("../figures/s2_pop_gc.pdf", width = 5 height = 5)
## background
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, length(proml))
#		, ylim = c(0,10000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = "Mercosur's Popular Support (%)"
#		, ylab = "Expected Incorporation Duration (days)"
#		)
##axes etc.
#axis(1
#		, at = c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))
#		, lab = c('70', '75','80', '85','90')
#		)
#axis(2, at= c(0,2000, 4000, 6000, 8000, 10000), las = 1)
## Grids
##for (i in seq(0, length(proml), .25* length(proml))){lines(c(i,i),c(-200,12000), col = "grey50", lty = 3)}  #vertical grid lines
##for (i in seq(0, 10000, 2500)){lines(c(-5,length(proml)),c(i,i), col = "grey50", lty = 3)}	# hor. grid lines
## Add the shades
#add.shade(weib.pop.gc.cmc.s, proml, oxblue)
#lines(apply(weib.pop.gc.cmc.s$qi$ev, 2, low)[0:40], col = 'white' , lwd = 3, lty = 1)	
#add.shade(weib.pop.gc.ncmc.s, proml, oxgrey)
#lines(apply(weib.pop.gc.cmc.s$qi$ev, 2, low)[0:80], col = 'white' , lwd = 3, lty = 3)	
#lines(apply(weib.pop.gc.cmc.s$qi$ev, 2, mean), col = 'white' , lwd = 3, lty = 2)	
##add.shade(weib.pop.tc.s, pop, oxgrey)
#
#legend(18, 9650
#			, box.col = 'white'
#			, col = c(oxblue, oxgrey)
#			, pch = 15
#			, bg = 'white'
#			, legend = c("CMC", "CMG & TC")
#			)
#
#dev.off()



# Plot Government Cooperation FD
#pdf("../figures/s2_pop_gc_fd.pdf", width = 5, height = 5)
#par(mar = c(5,5,1,1))
## background
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, length(proml))
#		, ylim = c(-4000,12000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = "Mercosur's Popular Support (%)"
#		, ylab = "Expected Duration Difference (days)"
#		)
##axes etc.
#axis(1
#		, at = c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))
#		, lab = c('70', '75','80', '85','90')
#		)
#axis(2, at= c(-4000,0, 4000, 8000, 12000), las = 1)	
## Grids
##for (i in c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))){lines(c(i,i),c(-5000,13000), col = "grey50", lty = 3)}  #vertical grid lines
##for (i in c(-4000,0, 4000, 8000, 12000)){lines(c(-5,length(proml)),c(i,i), col = "grey50", lty = 3)}	# hor. grid lines
## Add the shades
#add.shade.fd(weib.pop.gc.fd.s, proml, oxblue)
#
#dev.off()






# Plot Trade Exception
#pdf("../figures/s2_pop_te.pdf", width = 5, height = 5)
#par(mar = c(5,5,1,1))
## background
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, length(proml))
#		, ylim = c(0,10000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = "Mercosur's Popular Support (%)"
#		, ylab = "Expected Incorporation Duration (days)"
#		)
##axes etc.
#axis(1
#		, at = c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))
#		, lab = c('70', '75','80', '85','90')
#		)
#axis(2, at= c(0,2500, 5000, 7500, 10000), las = 1)		
## Grids
##for (i in seq(0, length(proml), .25* length(proml))){lines(c(i,i),c(-5000,12000), col = "grey50", lty = 3)}  #vertical grid lines
##for (i in seq(0, 10000, 2500)){lines(c(-5,length(proml)),c(i,i), col = "grey50", lty = 3)}	# hor. grid lines
## Add the shades

	
#add.shade(weib.pop.te.ncmc.s, proml, oxgrey)
#lines(apply(weib.pop.te.ncmc.s$qi$ev, 2, low), col = 'white' , lwd = 3, lty = 1)	
#add.shade(weib.pop.te.cmc.s, proml, oxblue)
#lines(apply(weib.pop.te.ncmc.s$qi$ev, 2, high)[1:50], col = 'white' , lwd = 3, lty = 3)	
#lines(apply(weib.pop.te.ncmc.s$qi$ev, 2, mean), col = 'white' , lwd = 3, lty = 2)	
#lines(apply(weib.pop.te.ncmc.s$qi$ev, 2, low), col = 'white' , lwd = 3, lty = 3)	
##add.shade(weib.pop.tc.s, pop, oxgrey)
#
#legend(18, 9650
#			, box.col = 'white'
#			, col = c(oxblue, oxgrey)
#			, pch = 15
#			, bg = 'white'
#			, legend = c("CMC", "CMG & TC")
#			)
#dev.off()
#




## Plot Trade Exception FD
##pdf("../figures/s2_pop_te_fd_new.pdf", width = 5, height = 5)
#pdf("../figures/1fd.pdf", width = 5, height = 5)
#par(mar = c(5,5,1,1))
#plot(1,1, type = "n"
#		, bty = "n"
#		, xlim = c(0, length(proml))
#		, ylim = c(-4000,12000)
#		, xaxt = "n" , yaxt = "n"
#		, xlab = "Mercosur's Popular Support (%)"
#		, ylab = "Expected Duration Difference (days)"
#		)
##axes etc.
#axis(1
#		, at = c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))
#		, lab = c('70', '75','80', '85','90')
#		)
#axis(2, at= c(-4000,0, 4000, 8000, 12000), las = 1)		
## Grids
##for (i in c(0, .25*length(proml), .5*length(proml), .75*length(proml), length(proml))){lines(c(i,i),c(-5000,13000), col = "grey50", lty = 3)}  #vertical grid lines
##for (i in c(-4000,0, 4000, 8000, 12000)){lines(c(-5,length(proml)),c(i,i), col = "grey50", lty = 3)}	# hor. grid lines
## Add the shades
#add.shade.fd(weib.pop.te.fd.s, proml, oxblue)
#
#dev.off()
#######################################################












